package Easy多数元素;

import org.testng.annotations.Test;

import java.util.*;

public class Solution {
    public int majorityElement(int[] nums) {
        int max=0,num=0;
        Map<Integer,Integer> map = new HashMap<>();
        Set<Integer> set = new HashSet<>();
        for (int i:
                nums) {
            if (!map.containsKey(i)){
                set.add(i);
                map.put(i,1);
            }
            else
                map.put(i,map.get(i)+1);
        }
        for (int i:
                set) {
            int count = map.get(i);
            if (count>max) {
                max = count;
                num = i;
            }
        }
        return num;
    }
    public int majorityElement1(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length / 2];
    }

    @Test
    public void test(){
        int[] nums = {2};
        System.out.println(majorityElement(nums));
    }
}
